package engine.asset
{
	import engine.core.*;
	import engine.loaders.*;
	import flash.system.*;
	
	public class RxAssetGroup extends RxAsset
	{
		private var m_xml:XML;
		private var m_assets:Vector.<RxAsset>;
		private var m_numActive:int;
		private var m_numErrors:int;
		private static var s_type:int;
		private static const STATE_XMLREADY:int = 4;
		private static const STATE_LOADINGASSETS:int = 5;
		
		public function RxAssetGroup()
		{
			super(s_type);
			this.m_xml = null;
			this.m_assets = new Vector.<RxAsset>;
		}
		
		public static function Initialize() : void
		{
			s_type = RxAsset.RegisterType("assetgroup", CreateFromXML);
		}
		
		public static function CreateFromXML(xml:XML, info:String) : RxAssetGroup
		{
			var name:* = xml.@name;
			var url:* = xml.@url;
			url = RxUtilities.MergePath(info, url);
			var group:RxAssetGroup = new RxAssetGroup();
			group.SetURL(url);
			group.SetName(name);
			return group;
		}
		
		public function Init(param1:XML, param2:String) : void
		{
			var _loc_3:XML = null;
			var _loc_4:String = null;
			var _loc_5:Function = null;
			var _loc_6:RxAsset = null;
			this.m_xml = param1;
			m_state = STATE_XMLREADY;
			for each (_loc_3 in param1.*)
			{
				
				_loc_4 = _loc_3.name();
				_loc_5 = RxAsset.GetCreateFromXMLFunction(_loc_4);
				if (_loc_5 != null)
				{
					_loc_6 = _loc_5(_loc_3, m_url);
					if (_loc_6 != null)
					{
						this.AddAsset(_loc_6);
					}
					continue;
				}
				RxLog.Error("Could not find CreateFromXML() function for asset type : " + _loc_4);
			}
		}
		
		public function AddAsset(param1:RxAsset) : void
		{
			this.m_assets.push(param1);
		}
		
		public function GetAsset(param1:int) : RxAsset
		{
			if (param1 < 0 || param1 >= this.m_assets.length)
			{
				return null;
			}
			return this.m_assets[param1];
		}
		
		public function GetXML() : XML
		{
			return this.m_xml;
		}
		
		override public function Load() : void
		{
			super.Load();
			this.LoadAssets();
		}
		
		private function LoadAssets() : void
		{
			var _loc_1:RxAsset = null;
			if (m_state == STATE_EMPTY && m_url == null)
			{
				m_state = STATE_XMLREADY;
			}
			switch(m_state)
			{
				case STATE_EMPTY:
				{
					m_loader = StartLoad(m_url, RxLoader.TYPE_TEXT, this.OnXMLLoaded);
					m_state = STATE_LOADING;
					break;
				}
				case STATE_LOADING:
				{
					break;
				}
				case STATE_XMLREADY:
				{
					if (this.m_assets.length == 0)
					{
						ExecuteCallbacks(true);
						m_state = STATE_READY;
						break;
					}
					m_state = STATE_LOADINGASSETS;
					this.m_numActive = this.m_assets.length;
					this.m_numErrors = 0;
					for each (_loc_1 in this.m_assets)
					{
						
						_loc_1.AddCallback(new RxAssetCallback(this.OnGroupItemLoaded));
						_loc_1.SetUsedByGPU(m_usedByGPU);
						if (m_suppressIOError)
						{
							_loc_1.SuppressIOError();
						}
						_loc_1.SetProfiler(m_profiler);
						_loc_1.Load();
					}
					break;
				}
				case STATE_LOADINGASSETS:
				{
					break;
				}
				case STATE_ERROR:
				{
					ExecuteCallbacks(false);
					break;
				}
				case STATE_READY:
				{
					ExecuteCallbacks(true);
					break;
				}
				default:
				{
					break;
				}
			}
		}
		
		private function OnXMLLoaded(param1:RxLoaderCallback) : void
		{
			var _loc_2:XML = null;
			if (param1.m_result == true)
			{
				m_size = param1.m_loader.GetSizeInBytes();
				_loc_2 = XML(param1.m_loader.GetText());
				this.Init(_loc_2, m_url);
				this.LoadAssets();
			}
			else
			{
				m_state = STATE_EMPTY;
				ExecuteCallbacks(false);
			}
		}
		
		private function OnGroupItemLoaded(param1:RxAssetCallback) : void
		{
			if (param1.m_result == false)
			{
				//this.m_numErrors = this.m_numErrors + 1;
			}
			
			m_numActive = this.m_numActive - 1;
			
			if (this.m_numActive == 0)
			{
				if (this.m_numErrors === 0)
				{
					m_state = STATE_READY;
					ExecuteCallbacks(true);
				}
				else
				{
					m_state = STATE_ERROR;
					ExecuteCallbacks(false);
				}
			}
		}
	}
}